package com.ruoyi.zlgl.zlsb.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.infoHiComment.domain.InfoHiComment;
import com.ruoyi.infoHiComment.mapper.InfoHiCommentMapper;
import com.ruoyi.infoRunTaskData.domain.InfoRunTaskData;
import com.ruoyi.infoRunTaskData.service.IInfoRunTaskDataService;
import com.ruoyi.zlgl.zlsb.domain.ZlglZlsb;
import com.ruoyi.zlgl.zlsb.domain.ZlglZlsbFj;
import com.ruoyi.zlgl.zlsb.service.IZlglZlsbService;
import com.ruoyi.zlgl.zlsb.vo.ZlSqVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.*;

/**
 * 专利申报Controller
 *
 * @author huacai
 * @date 2024-08-15
 */
@RestController
@RequestMapping("/zlsb/zlsb")
public class ZlglZlsbController extends BaseController {
    @Resource
    private IZlglZlsbService zlglZlsbService;

    @Resource
    private IInfoRunTaskDataService iInfoRunTaskDataService;

    @Resource
    InfoHiCommentMapper infoHiCommentMapper;

    /**
     * 查询专利申报列表
     */
    @PreAuthorize("@ss.hasPermi('zlsb:zlsb:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZlglZlsb zlglZlsb) {
        startPage();
        List<ZlglZlsb> list = zlglZlsbService.selectZlglZlsbList(zlglZlsb);
        return getDataTable(list);
    }

    /**
     * 查询专利申报列表 (无数据权限校验)
     */
    @GetMapping("/zlList")
    public TableDataInfo selectZlList(ZlglZlsb zlglZlsb) {
        startPage();
        List<ZlglZlsb> list = zlglZlsbService.selectZlList(zlglZlsb);
        return getDataTable(list);
    }

    /**
     * 查询专利流程状态为授权(正常)的专利
     */
    @GetMapping("/selectZlListBySq")
    public TableDataInfo selectZlListBySq(ZlglZlsb zlglZlsb) {
        startPage();
        List<ZlglZlsb> list = zlglZlsbService.selectZlListBySq(zlglZlsb);
        return getDataTable(list);
    }

    /**
     * 查询专利流程状态为
     * 审批通过  国知产权局申请 知产申请驳回  授权(正常) 授权(失效) 的数据
     * 用于授权登记页面
     */
    @GetMapping("/selectZlBySqDj")
    public TableDataInfo selectZlBySqDj(ZlglZlsb zlglZlsb) {
        startPage();
        List<ZlglZlsb> list = zlglZlsbService.selectZlBySqDj(zlglZlsb);
        return getDataTable(list);
    }

    /**
     * 导出专利申报列表
     */
    @PreAuthorize("@ss.hasPermi('zlsb:zlsb:export')")
    @Log(title = "专利申报", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZlglZlsb zlglZlsb) {
        List<ZlglZlsb> list = zlglZlsbService.selectZlglZlsbList(zlglZlsb);
        ExcelUtil<ZlglZlsb> util = new ExcelUtil<ZlglZlsb>(ZlglZlsb.class);
        util.exportExcel(response, list, "专利申报数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<ZlglZlsb> util = new ExcelUtil<ZlglZlsb>(ZlglZlsb.class);
        util.importTemplateExcel(response, "专利申报数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "专利申报", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('zlsb:zlsb:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<ZlglZlsb> util = new ExcelUtil<ZlglZlsb>(ZlglZlsb.class);
        InputStream inputStream = file.getInputStream();
        List<ZlglZlsb> list = util.importExcel(inputStream);
        inputStream.close();
        int count = zlglZlsbService.batchInsertZlglZlsb(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取专利申报详细信息
     */
    @PreAuthorize("@ss.hasPermi('zlsb:zlsb:query')")
    @GetMapping(value = "/{zlsbId}")
    public AjaxResult getInfo(@PathVariable("zlsbId") String zlsbId) {
        return success(zlglZlsbService.selectZlglZlsbByZlsbId(zlsbId));
    }

    /**
     * 新增专利申报
     */
    @PreAuthorize("@ss.hasPermi('zlsb:zlsb:add')")
    @Log(title = "专利申报", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZlglZlsb zlglZlsb) {
        zlglZlsb.setUserId(Math.toIntExact(getUserId()));
        int i = zlglZlsbService.insertZlglZlsb(zlglZlsb);
        //向流程业务表中加入数据
        InfoRunTaskData infoRunTaskData = new InfoRunTaskData();
        infoRunTaskData.setBusinessKey(zlglZlsb.getZlsbId());
        infoRunTaskData.setBusinessType(10); //业务模块: 专利管理-专利申报
        infoRunTaskData.setBusinessTitle(zlglZlsb.getZlName()); //任务名称: 专利名称
        infoRunTaskData.setApplyUser(SecurityUtils.getLoginUser().getUser().getNickName());//申请人

        iInfoRunTaskDataService.insertInfoRunTaskData(infoRunTaskData);

        return toAjax(i);
    }

    /**
     * 修改专利申报
     */
    @PreAuthorize("@ss.hasPermi('zlsb:zlsb:edit')")
    @Log(title = "专利申报", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZlglZlsb zlglZlsb) {
        //修改业务中间表关系数据
        InfoRunTaskData infoRunTaskData = new InfoRunTaskData();
        infoRunTaskData.setBusinessTitle(zlglZlsb.getZlName()); //任务名称: 专利名称
        infoRunTaskData.setBusinessKey(zlglZlsb.getZlsbId());
        infoRunTaskData.setBusinessType(10); //业务模块: 专利管理-专利申报
        iInfoRunTaskDataService.updateInfoRunTaskDatas(infoRunTaskData); //注意updateInfoRunTaskDatas

        return toAjax(zlglZlsbService.updateZlglZlsb(zlglZlsb));
    }

    /**
     * 专利 国知产权局申请 信息填写
     */
    @Log(title = "专利申报", businessType = BusinessType.UPDATE)
    @PutMapping("/zlSqUpdate/{processInstanceId}")
    public AjaxResult zlSqUpdate(@RequestBody ZlglZlsb zlglZlsb, @PathVariable("processInstanceId") String processInstanceId) {
        //修改业务中间表关系数据
        InfoRunTaskData infoRunTaskData = new InfoRunTaskData();
        infoRunTaskData.setBusinessTitle(zlglZlsb.getZlName()); //任务名称: 专利名称
        infoRunTaskData.setBusinessKey(zlglZlsb.getZlsbId());
        infoRunTaskData.setBusinessType(10); //业务模块: 专利管理-专利申报
        iInfoRunTaskDataService.updateInfoRunTaskDatas(infoRunTaskData); //注意updateInfoRunTaskDatas

        //修改业务数据状态为完成 5国知产权局申请
        iInfoRunTaskDataService.updateStatus(zlglZlsb.getZlsbId(), 10, 5);
        //新增历史记录
        InfoHiComment infoHiComment = new InfoHiComment();
        infoHiComment.setCreateTime(new Date());
        infoHiComment.setCreateName(SecurityUtils.getLoginUser().getUser().getNickName());
        infoHiComment.setMessage("提交专利申请");
        infoHiComment.setTaskName("提交专利申请");
        infoHiComment.setProcessInstanceId(processInstanceId);
        infoHiCommentMapper.insertInfoHiComment(infoHiComment);

        return toAjax(zlglZlsbService.updateZlglZlsb(zlglZlsb));
    }

    /**
     * 专利 授权成果 信息填写
     */
    @Log(title = "专利申报", businessType = BusinessType.UPDATE)
    @PutMapping("/zlSqcgUpdate/{processInstanceId}")
    public AjaxResult zlSqcgUpdate(@RequestBody ZlglZlsb zlglZlsb, @PathVariable("processInstanceId") String processInstanceId) {
        //修改业务中间表关系数据
        InfoRunTaskData infoRunTaskData = new InfoRunTaskData();
        infoRunTaskData.setBusinessTitle(zlglZlsb.getZlName()); //任务名称: 专利名称
        infoRunTaskData.setBusinessKey(zlglZlsb.getZlsbId());
        infoRunTaskData.setBusinessType(10); //业务模块: 专利管理-专利申报
        iInfoRunTaskDataService.updateInfoRunTaskDatas(infoRunTaskData); //注意updateInfoRunTaskDatas

        //修改业务数据状态为完成 7授权(正常)
        iInfoRunTaskDataService.updateStatus(zlglZlsb.getZlsbId(), 10, 7);
        //新增历史记录
        InfoHiComment infoHiComment = new InfoHiComment();
        infoHiComment.setCreateTime(new Date());
        infoHiComment.setCreateName(SecurityUtils.getLoginUser().getUser().getNickName());
        infoHiComment.setMessage("专利已授权");
        infoHiComment.setTaskName("专利申请成功");
        infoHiComment.setProcessInstanceId(processInstanceId);
        infoHiCommentMapper.insertInfoHiComment(infoHiComment);

        return toAjax(zlglZlsbService.updateZlglZlsb(zlglZlsb));
    }


    /**
     * 删除专利申报
     */
    @PreAuthorize("@ss.hasPermi('zlsb:zlsb:remove')")
    @Log(title = "专利申报", businessType = BusinessType.DELETE)
    @DeleteMapping("/{zlsbIds}")
    public AjaxResult remove(@PathVariable String[] zlsbIds) {
        //同时删除业务关系数据
        for (String zlsbId : zlsbIds) {
            ZlglZlsb zlglZlsb = zlglZlsbService.selectZlglZlsbByZlsbId(zlsbId);
            iInfoRunTaskDataService.deleteData(zlglZlsb.getZlsbId(), 10);//业务模块: 专利管理-专利申报
        }
        return toAjax(zlglZlsbService.deleteZlglZlsbByZlsbIds(zlsbIds));
    }

    /**
     * 获得专利申报主键ID (UUID)
     */
    @GetMapping("/getZlSbId")
    public AjaxResult getZlSbId() {
        String zlsbId = IdUtils.fastSimpleUUID();
        return success(zlsbId);
    }

    /**
     * 新增附件
     */
    @PostMapping("/add/File/By/subId")
    public AjaxResult insertZlsbFjByZlsbId(@RequestBody ZlglZlsbFj zlglZlsbFj) {
        return toAjax(zlglZlsbService.insertZlsbFjByZlsbId(zlglZlsbFj));
    }

    /**
     * 查询附件列表
     */
    @GetMapping("/get/FileList/By/subId")
    public TableDataInfo selectZlsbFjList(ZlglZlsbFj zlglZlsbFj) {
        startPage();
        List<ZlglZlsbFj> list = zlglZlsbService.selectZlsbFjList(zlglZlsbFj);
        return getDataTable(list);
    }

    /**
     * 删除专利附件
     */
    @Log(title = "删除专利附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/zlsb/{fjIds}")
    public AjaxResult remove(@PathVariable Integer[] fjIds) {
        return toAjax(zlglZlsbService.deleteZlsbFjByFjIds(fjIds));
    }

    /**
     * 专利授权趋势
     *
     * @return
     */
    @GetMapping("/getSqQs")
    public AjaxResult getSqQs() {

        List<ZlSqVo> zlSqVo = zlglZlsbService.getSqQs();

        // 创建一个Map来保存结果数据
        Map<String, Object> resultMap = new HashMap<>();
        List<String> fmData = new ArrayList<>();
        List<String> syData = new ArrayList<>();
        List<String> wgData = new ArrayList<>();
        List<String> yearData = new ArrayList<>();

        for (ZlSqVo sqVo : zlSqVo) {
            yearData.add(sqVo.getSqNd());
            fmData.add(sqVo.getFmxzlCount());
            syData.add(sqVo.getSyzlCount());
            wgData.add(sqVo.getWgzlCount());
        }

        // 添加数据到map中
        resultMap.put("fmData", fmData);
        resultMap.put("syData", syData);
        resultMap.put("wgData", wgData);
        resultMap.put("yearData", yearData);

        return AjaxResult.success(resultMap);
    }

}
